<?php
declare (strict_types=1);

namespace app\v1\controller;

use app\BaseController;
use app\common\Express;
use app\model\Config;
use app\Request;
use app\model\Banner;
use app\model\Goods;
use app\model\GoodsClass;
use app\model\GoodsOrder;
use app\model\User;
use GuzzleHttp\Client;
use think\db\Query;

class IndexController extends BaseController
{
	
	protected $middleware = ['Login' => ['except' => ['test', 'index', 'banner', 'class' , 'list']]];
	
	function test()
	{
	
	}
	
	function index(Request $request)
	{
		$rows = GoodsOrder::select();
		writeJson(1, '请求成功', $rows);
	}
	
	/**
	 *  轮播图
	 * @return void
	 */
	function banner()
	{
		$rows = Banner::order(['weigh' => 'desc'])->select();
		writeJson($rows);
	}
	
	/**
	 * 分类
	 * @return void
	 */
	function class()
	{
		$rows = GoodsClass::order(['weigh' => 'desc'])->select();
		writeJson($rows);
	}
	
	/**
	 * 列表
	 * @return void
	 */
	function list(Request $request,int $class_id, string $date, string $keyword, string $price, string $sales)
	{
	
		$user = User::find($request->user_id);
		$rows = Goods::when(!empty($date), function (Query $query) use ($date) {
			$query->whereDay('createtime', $date);
		})
			->when(!empty($class_id), function ($query) use ($class_id) {
				$query->where('class_id', $class_id);
			})
			->when(!empty($keyword), function ($query) use ($keyword) {
				$query->where('name', 'like', "%$keyword%");
			})
			->when(in_array($price, ['asc', 'desc']) || in_array($sales, ['asc', 'desc']), function ($query) use ($price, $sales) {
				if (!empty($price)) {
					$query->order('price', $price);
				}
				if (!empty($sales)) {
					$query->order('sales', $sales);
				}
			}, function ($query) {
				$query->order('id', 'desc');
			})
			->when($request->user_id == 0||(!empty($user)&&$user->identity == 1),function (Query $query){
				$query->where(['status'=>1]);
			})
			->paginate()
			->items();
		writeJson($rows);
	}
	
	/**
	 * 详情
	 * @param Request $request
	 * @param int $id
	 * @param string $invite_code
	 * @return void
	 */
	function read(Request $request, int $id, string $invite_code)
	{
		
		$user = User::find($request->user_id);
		$row = Goods::with(['assess' => function (Query $query) {
			$query->with(['user','orders'=>function (Query $query) {
				$query->with(['spec']);
			}])->limit(5);
		}])
			->withCount('assess')
			->withCount(['assess' => function (Query $query, &$alias) {
				$query->where(['score' => 5]);
				$alias = 'assess_good_count';
			}])
			->withCount(['assess'=>function (Query $query,&$alias) {
				$query->whereIn('score',[3,4]);
				$alias = 'assess_normal_count';
			}])
			->withCount(['assess'=>function (Query $query,&$alias) {
				$query->whereIn('score',[1,2]);
				$alias = 'assess_bad_count';
			}])
			->withCount(['assess'=>function (Query $query,&$alias) {
				$query->where('images','<>','');
				$alias = 'assess_images_count';
			}])
			->filter(function ($data) use ($user, $invite_code) {
				$info = [];
				$data->invite_code = $invite_code;
				if ($user->level != 0 && !empty($invite_code)) {
					$price = $data->price;
					$hand_price = $price - $data->{'level_' . $user->level};
					$difference_price = $data->{'level_' . $user->level};
					$sales = $data->sales;
					$sku = $data->sku;
					$info = [
						'price' => $price,
						'hand_price' => $hand_price,
						'difference_price' => $difference_price,
						'sales' => $sales,
						'sku' => $sku
					];
				}
				$data->info = $info;
			})->findOrFail($id);
		writeJson($row);
	}
	
	
}
